/* ---- Global variables ---- */
/* When comparing between two sets for both (in place elements) and (not in place elements), checked is a
 * boolean array indicates the elements that has been marked as (in place)
 */
var checked;
// In Place Image and Not In Place Image
var ipImage = 'images/CB.png';
var nipImage = 'images/CG.png';
var blankImage = 'images/CW.png';



/* 
 * Method to compare two sets and return a numerical value that indicates how many matched symbols
 * that has the same place in the two sets, also how many matched symbols but not in the same place
 * return an integer value of two digits
 * the ((digit on the left)  - 1) indicates the number of the symbols that are not in place
 * the ((digit on the right) - 1) indicates the number of the symbols that are in place
 * Return : int
 */
function compareSets(newSet, origin) {
	initCheckedFlags(newSet);
	return Number(getNotInPlace(newSet, origin) + getInPlace(newSet, origin));
}

/*
 * Method to compare two sets and return a numerical value that indicates how many matched symbols
 * that has the same place in the two sets, where the (return value -1) represents the number of the symbols that are in place
 * Return : int
 */
function getInPlace(newSet, origin) {
	initCheckedFlags(newSet);
	var ip = 1;
	for( var i = 0 ; i < newSet.length ; i++ ) {
		if( isBothInPlace(newSet, origin, i) ) {
			checked[i] = true;
			ip++;
		}
	}
	return ip;
}

/*
 * Method to compare two sets and return a numerical value that indicates how many matched symbols 
 * but not in the same place, where the (left digit of the return value -1) represents the number of the symbols that are in place
 * ( [left degit] - 1 = total matched items not in place )
 * Return : int
 */
function getNotInPlace(newSet, origin) {
	initCheckedFlags(newSet);
	var nip = 10;
	for( var i = 0 ; i < newSet.length ; i++ ) {
		if( isBothInPlace(newSet, origin, i ) ) {
			continue;
		}
		var symbolIndecies = getSymbolIndecies(newSet.charAt(i), origin, i);
		if( symbolIndecies != null && symbolIndecies.length > 0 ) {
			for( var n = 0 ; n < symbolIndecies.length ; n++ ) {
				if( !isBothInPlace(newSet, origin, symbolIndecies[n]) && !checked[symbolIndecies[n]] ) {
					checked[symbolIndecies[n]] = true;
					nip += 10;
					break;
				}
			}
		}
	}
	return nip;
}

/*
 * Like method indexOf() but this instead return all the matched indexes as an Array
 * Return : Array of int
 */
function getSymbolIndecies(symbol, origin, index) {
	var buffer = '';
	var symbols = new Array();
	for( var i = 0 ; i < origin.length ; i++ ) {
		if( origin.charAt(i) == symbol ) {
			buffer += i;
		}
	}
	if( buffer.length > 0 ) {
		symbols = new Array(buffer.length);
		for( var i = 0 ; i < symbols.length ; i++ ) {
			symbols[i] = Number(buffer.charAt(i));
		}
	}
	return symbols;
}

/*
 * Method to check if the selected index have the same symbols in two sets
 * Return : boolean
 */
function isBothInPlace(newSet, origin, index) {
	return (newSet.charAt(index) == origin.charAt(index));
}
 
/*
 * Method translate the result came from compareSets() method to only matched symbols in place
 * Return : int
 */
function getResultInPlace(result) {
	return Math.floor(result % 10 - 1);
}

/*
 * Method translate the result came from compareSets() method to only matched symbols not in place
 * Return : int
 */
function getResultNotInPlace(result) {
	//alert(result);
	return Math.floor((result - getResultInPlace(result))/10 -1);
}

/*
 * Method checks whether the checkedFlags have been initialized, and initiate them
 * if they were already initialized, reset them
 * Return : void
 */
function initCheckedFlags(newSet) {
	if(!checked) {
		checked = new Array(newSet.length);
	}
	for(var i = 0 ; i < checked.length ; i++) {
		checked[i] = false;
	}
}

/*
 * Adapter that takes the tow sets, and return the result as a sequence of images
 * Return : string
 */
function adaptToImages(newSet, origin) {
	var result = '';
	var total = 0;
	for( var i = 0 ; i < getResultInPlace(compareSets(newSet, origin)) ; i++ ) {
		total++;
		result += '<img src="' + ipImage + '" />';
	}
	for( var m = 0 ; m < getResultNotInPlace(compareSets(newSet, origin)) ; m++ ) {
		total++;
		result += '<img src="' + nipImage + '" />';
	}
	if( total < 4 ) {
		for( var t = total+1 ; t <= 4 ; t++ ) {
			result += '<img src="' + blankImage + '" />';
		}
	}
	return result;
}